home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / STRNCPY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  502 b   |  20 lines

  1. /* strncpy.c  From TC Bible page 291  Use strncpy to copy a specified number
  2.  of characters of one string to another. */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. main()
  7. {
  8.     size_t len;
  9.     char str1[80], str2[80];
  10.     printf("Enter a string: ");
  11.     gets(str2);
  12.     len = strlen(str2)/2;
  13.     strncpy(str1, str2, len);
  14.  
  15.     /* Since '\0' is not appended automatically, we have to do so before
  16.        printing string */
  17.  
  18.     str1[len] = '\0';
  19.     printf("Halft the length of string copied. Result:\%s\n", str1);
  20. }